"""
System level test for arbitrary waveform

---

- Copyright (C) 2018 - PRESENT  radarsimx.com
- E-mail: info@radarsimx.com
- Website: https://radarsimx.com

::

    ██████╗  █████╗ ██████╗  █████╗ ██████╗ ███████╗██╗███╗   ███╗██╗  ██╗
    ██╔══██╗██╔══██╗██╔══██╗██╔══██╗██╔══██╗██╔════╝██║████╗ ████║╚██╗██╔╝
    ██████╔╝███████║██║  ██║███████║██████╔╝███████╗██║██╔████╔██║ ╚███╔╝
    ██╔══██╗██╔══██║██║  ██║██╔══██║██╔══██╗╚════██║██║██║╚██╔╝██║ ██╔██╗
    ██║  ██║██║  ██║██████╔╝██║  ██║██║  ██║███████║██║██║ ╚═╝ ██║██╔╝ ██╗
    ╚═╝  ╚═╝╚═╝  ╚═╝╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═╝╚══════╝╚═╝╚═╝     ╚═╝╚═╝  ╚═╝

"""

import numpy as np

from radarsimpy import Radar, Transmitter, Receiver
from radarsimpy.simulator import sim_radar  # pylint: disable=no-name-in-module


def test_arbitrary_waveform():
    """
    This function tests the arbitrary waveform.
    """
    tx_channel = {
        "location": (0, 0, 0),
    }

    freq_nonlinear = np.array(
        [
            2.40750000e10,
            2.40760901e10,
            2.40771786e10,
            2.40782654e10,
            2.40793506e10,
            2.40804341e10,
            2.40815161e10,
            2.40825964e10,
            2.40836750e10,
            2.40847521e10,
            2.40858275e10,
            2.40869012e10,
            2.40879734e10,
            2.40890439e10,
            2.40901127e10,
            2.40911800e10,
            2.40922456e10,
            2.40933096e10,
            2.40943719e10,
            2.40954326e10,
            2.40964917e10,
            2.40975491e10,
            2.40986049e10,
            2.40996591e10,
            2.41007117e10,
            2.41017626e10,
            2.41028119e10,
            2.41038595e10,
            2.41049055e10,
            2.41059499e10,
            2.41069927e10,
            2.41080338e10,
            2.41090733e10,
            2.41101111e10,
            2.41111473e10,
            2.41121819e10,
            2.41132149e10,
            2.41142462e10,
            2.41152759e10,
            2.41163039e10,
            2.41173304e10,
            2.41183552e10,
            2.41193783e10,
            2.41203999e10,
            2.41214198e10,
            2.41224380e10,
            2.41234546e10,
            2.41244696e10,
            2.41254830e10,
            2.41264947e10,
            2.41275048e10,
            2.41285133e10,
            2.41295202e10,
            2.41305254e10,
            2.41315289e10,
            2.41325309e10,
            2.41335312e10,
            2.41345298e10,
            2.41355269e10,
            2.41365223e10,
            2.41375161e10,
            2.41385082e10,
            2.41394987e10,
            2.41404876e10,
            2.41414748e10,
            2.41424605e10,
            2.41434444e10,
            2.41444268e10,
            2.41454075e10,
            2.41463866e10,
            2.41473640e10,
            2.41483399e10,
            2.41493140e10,
            2.41502866e10,
            2.41512575e10,
            2.41522268e10,
            2.41531945e10,
            2.41541605e10,
            2.41551249e10,
            2.41560876e10,
            2.41570488e10,
            2.41580083e10,
            2.41589661e10,
            2.41599224e10,
            2.41608770e10,
            2.41618299e10,
            2.41627812e10,
            2.41637309e10,
            2.41646790e10,
            2.41656254e10,
            2.41665702e10,
            2.41675134e10,
            2.41684550e10,
            2.41693949e10,
            2.41703331e10,
            2.41712698e10,
            2.41722048e10,
            2.41731381e10,
            2.41740699e10,
            2.41750000e10,
        ]
    )

    tx_nonlinear = Transmitter(
        f=freq_nonlinear,
        t=np.linspace(0, 80e-6, 100),
        tx_power=40,
        prp=100e-6,
        pulses=1,
        channels=[tx_channel],
    )

    rx_channel = {
        "location": (0, 0, 0),
    }

    rx = Receiver(
        fs=2e5,
        noise_figure=12,
        rf_gain=20,
        load_resistor=500,
        baseband_gain=30,
        channels=[rx_channel],
    )

    radar_nonlinear = Radar(transmitter=tx_nonlinear, receiver=rx)

    targets = [{"location": (200, 0, 0), "speed": (-5, 0, 0), "rcs": 20, "phase": 0}]

    result = sim_radar(radar_nonlinear, targets)

    assert np.allclose(
        result["baseband"],
        np.array(
            [
                [
                    [
                        2.18508231e-03 + 0.00029834j,
                        1.94347688e-03 + 0.00104235j,
                        2.15462260e-03 - 0.00047032j,
                        2.74110357e-04 - 0.00218826j,
                        -2.20141465e-03 + 0.00013182j,
                        1.53208282e-03 + 0.00158629j,
                        -8.78069136e-04 - 0.00202302j,
                        1.23647175e-03 + 0.00182613j,
                        -2.12812091e-03 - 0.00057854j,
                        1.25044390e-03 - 0.00181659j,
                        2.01748455e-03 + 0.00089071j,
                        6.40810057e-04 + 0.00211021j,
                        -9.06653710e-05 + 0.0022035j,
                        3.22449600e-04 + 0.00218166j,
                        1.67007645e-03 + 0.0014403j,
                        1.90978212e-03 - 0.00110289j,
                    ]
                ]
            ]
        ),
    )

    assert np.allclose(
        result["timestamp"],
        np.array(
            [
                [
                    [
                        0.0e00,
                        5.0e-06,
                        1.0e-05,
                        1.5e-05,
                        2.0e-05,
                        2.5e-05,
                        3.0e-05,
                        3.5e-05,
                        4.0e-05,
                        4.5e-05,
                        5.0e-05,
                        5.5e-05,
                        6.0e-05,
                        6.5e-05,
                        7.0e-05,
                        7.5e-05,
                    ]
                ]
            ]
        ),
    )


def test_arbitrary_waveform_raytracing():
    """
    This function tests the arbitrary waveform.
    """
    tx_channel = {
        "location": (0, 0, 0),
    }

    freq_nonlinear = np.array(
        [
            2.40750000e10,
            2.40760901e10,
            2.40771786e10,
            2.40782654e10,
            2.40793506e10,
            2.40804341e10,
            2.40815161e10,
            2.40825964e10,
            2.40836750e10,
            2.40847521e10,
            2.40858275e10,
            2.40869012e10,
            2.40879734e10,
            2.40890439e10,
            2.40901127e10,
            2.40911800e10,
            2.40922456e10,
            2.40933096e10,
            2.40943719e10,
            2.40954326e10,
            2.40964917e10,
            2.40975491e10,
            2.40986049e10,
            2.40996591e10,
            2.41007117e10,
            2.41017626e10,
            2.41028119e10,
            2.41038595e10,
            2.41049055e10,
            2.41059499e10,
            2.41069927e10,
            2.41080338e10,
            2.41090733e10,
            2.41101111e10,
            2.41111473e10,
            2.41121819e10,
            2.41132149e10,
            2.41142462e10,
            2.41152759e10,
            2.41163039e10,
            2.41173304e10,
            2.41183552e10,
            2.41193783e10,
            2.41203999e10,
            2.41214198e10,
            2.41224380e10,
            2.41234546e10,
            2.41244696e10,
            2.41254830e10,
            2.41264947e10,
            2.41275048e10,
            2.41285133e10,
            2.41295202e10,
            2.41305254e10,
            2.41315289e10,
            2.41325309e10,
            2.41335312e10,
            2.41345298e10,
            2.41355269e10,
            2.41365223e10,
            2.41375161e10,
            2.41385082e10,
            2.41394987e10,
            2.41404876e10,
            2.41414748e10,
            2.41424605e10,
            2.41434444e10,
            2.41444268e10,
            2.41454075e10,
            2.41463866e10,
            2.41473640e10,
            2.41483399e10,
            2.41493140e10,
            2.41502866e10,
            2.41512575e10,
            2.41522268e10,
            2.41531945e10,
            2.41541605e10,
            2.41551249e10,
            2.41560876e10,
            2.41570488e10,
            2.41580083e10,
            2.41589661e10,
            2.41599224e10,
            2.41608770e10,
            2.41618299e10,
            2.41627812e10,
            2.41637309e10,
            2.41646790e10,
            2.41656254e10,
            2.41665702e10,
            2.41675134e10,
            2.41684550e10,
            2.41693949e10,
            2.41703331e10,
            2.41712698e10,
            2.41722048e10,
            2.41731381e10,
            2.41740699e10,
            2.41750000e10,
        ]
    )

    tx_nonlinear = Transmitter(
        f=freq_nonlinear,
        t=np.linspace(0, 80e-6, 100),
        tx_power=40,
        prp=100e-6,
        pulses=1,
        channels=[tx_channel],
    )

    rx_channel = {
        "location": (0, 0, 0),
    }

    rx = Receiver(
        fs=2e5,
        noise_figure=12,
        rf_gain=40,
        load_resistor=500,
        baseband_gain=30,
        channels=[rx_channel],
    )

    radar_nonlinear = Radar(transmitter=tx_nonlinear, receiver=rx)

    targets = [
        {
            "model": "./models/cr.stl",
            "location": (200, 0, 0),
        }
    ]

    result = sim_radar(radar_nonlinear, targets, density=1)

    assert np.allclose(
        result["baseband"],
        np.array(
            [
                [
                    [
                        0.00042601 - 2.93681645e-03j,
                        0.00148935 - 2.56677422e-03j,
                        -0.00046196 - 2.93141005e-03j,
                        -0.00290341 - 6.13889545e-04j,
                        -0.00014568 + 2.96402291e-03j,
                        0.00239102 - 1.75774516e-03j,
                        -0.00287518 + 7.34832121e-04j,
                        0.00272075 - 1.18493484e-03j,
                        -0.00135757 + 2.63884537e-03j,
                        -0.0019856 - 2.20539892e-03j,
                        0.00185658 - 2.31502889e-03j,
                        0.00296738 - 2.79537657e-05j,
                        0.0027857 + 1.02265162e-03j,
                        0.00291446 + 5.58266711e-04j,
                        0.00260711 - 1.41722114e-03j,
                        -0.00040577 - 2.93949706e-03j,
                    ]
                ]
            ]
        ),
    )

    assert np.allclose(
        result["timestamp"],
        np.array(
            [
                [
                    [
                        0.0e00,
                        5.0e-06,
                        1.0e-05,
                        1.5e-05,
                        2.0e-05,
                        2.5e-05,
                        3.0e-05,
                        3.5e-05,
                        4.0e-05,
                        4.5e-05,
                        5.0e-05,
                        5.5e-05,
                        6.0e-05,
                        6.5e-05,
                        7.0e-05,
                        7.5e-05,
                    ]
                ]
            ]
        ),
    )
